/*
 Copyright 2013 Tonic Artos

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

package com.tonicartos.widget.stickygridheaders;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import zj.com.model.InspectContent;
import zj.com.model.InspectItem;
import zj.com.zj.R;

/**
 * @author Tonic Artos
 * @param <T>
 */
public class StickyGridHeadersSimpleArrayAdapter<T> extends BaseAdapter implements
        StickyGridHeadersSimpleAdapter {
    protected static final String TAG = StickyGridHeadersSimpleArrayAdapter.class.getSimpleName();

    private int mHeaderResId;

    private LayoutInflater mInflater;

    private int mItemResId;

    private List<InspectContent> mItems;



    public Map<Integer,Boolean> checkMap = new HashMap<>();

    public StickyGridHeadersSimpleArrayAdapter(Context context, List<InspectContent> items, int headerResId,
            int itemResId) {
        init(context,items, headerResId, itemResId);
    }



    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public long getHeaderId(int position) {
        InspectContent item = getItem(position);


        return item.getInspectItemID();
    }

    @Override
    @SuppressWarnings("unchecked")
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(mHeaderResId, parent, false);
            holder = new HeaderViewHolder();
            holder.textView = (TextView)convertView.findViewById(android.R.id.text1);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder)convertView.getTag();
        }

        // set header text as first char in string



        return convertView;
    }

    @Override
    public InspectContent getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    @SuppressWarnings("unchecked")
    public View getView(final int position, View convertView, ViewGroup parent) {
      final  ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(mItemResId, parent, false);
            holder = new ViewHolder();
            holder.textView = (TextView)convertView.findViewById(android.R.id.text1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }




        holder.textView.setText(mItems.get(position).getInspectContentName());
        holder.textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if(checkMap.get(position) == null){
                    holder.textView.setBackgroundResource(R.drawable.grid_select);
                    holder.textView.setTextColor(Color.WHITE);
                    checkMap.put(position,true);
                }else{
                    Boolean b = checkMap.get(position);
                    if(b){
                        holder.textView.setBackgroundResource(R.drawable.grid_unselect);
                        holder.textView.setTextColor(Color.parseColor("#010101"));

                        checkMap.put(position, false);

                    }else{
                        holder.textView.setBackgroundResource(R.drawable.grid_select);
                        holder.textView.setTextColor(Color.WHITE);
                        checkMap.put(position,true);


                    }
                }
            }
        });


        return convertView;
    }

    private void init(Context context,List<InspectContent> items, int headerResId, int itemResId) {
        this.mItems = items;
        this.mHeaderResId = headerResId;
        this.mItemResId = itemResId;
        mInflater = LayoutInflater.from(context);

    }

    public void setDataChange(List<InspectContent> list){

        this.mItems = list;
        notifyDataSetChanged();

    }



    protected class HeaderViewHolder {
        public TextView textView;
    }

    protected class ViewHolder {
        public TextView textView;
    }
}
